/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.web.shell.config;

import cn.star.framework.rbac.entity.Role;
import cn.star.framework.rbac.entity.User;
import cn.star.framework.util.SpringContextUtil;
import com.xxl.adapter.entity.JobGroup;
import com.xxl.adapter.entity.JobLock;
import com.xxl.adapter.entity.JobUser;
import com.xxl.adapter.repository.JobGroupRepository;
import com.xxl.adapter.repository.JobLockRepository;
import com.xxl.adapter.repository.JobUserRepository;
import java.util.Date;
import org.springframework.transaction.annotation.Transactional;

/**
 * _default<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@SuppressWarnings("all")
public class _default {

  public static User getDefaultUser() {
    User user = new User();
    user.setId("1");
    user.setUsername("sa");
    user.setPassword("sa");
    user.setName("Administrator");
    user.setTenantId("1");

    return user;
  }

  public static Role getDefaultRole() {
    Role role = new Role();
    role.setId("1");
    role.setName("administrator");
    role.setCode("administrator");
    role.setTenantId("1");

    return role;
  }

  @Transactional(rollbackFor = Throwable.class)
  public static void xxl() {
    JobGroupRepository repository = SpringContextUtil.getBean(JobGroupRepository.class);
    JobUserRepository userRepository = SpringContextUtil.getBean(JobUserRepository.class);
    JobLockRepository lockRepository = SpringContextUtil.getBean(JobLockRepository.class);

    JobGroup root = new JobGroup();
    root.setId(1);
    root.setAppname("xxl-job-executor-sample");
    root.setTitle("示例执行器");
    root.setAddressType(0);
    root.setUpdateTime(new Date());

    root = repository.save(root);

    JobUser user = new JobUser();
    user.setId(1);
    user.setUsername("admin");
    user.setPassword("e10adc3949ba59abbe56e057f20f883e");
    user.setRole(root.getId());

    userRepository.save(user);

    JobLock lock = new JobLock();
    lock.setLockName("schedule_lock");

    lockRepository.save(lock);
  }
}
